package com.lbssoft.gpsbd.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lbssoft.datalink.util.Log;
import com.lbssoft.gpsbd.dao.PositionMapper;
import com.lbssoft.gpsbd.dao.StatisticsMapper;
import com.lbssoft.gpsbd.datalink.Context;
import com.lbssoft.gpsbd.model.Position;
import com.lbssoft.gpsbd.model.vo.Counts;
import com.lbssoft.gpsbd.service.StatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by Riven on 2018/4/3.
 */
@Service
public class StatisticsServiceImpl implements StatisticsService {
    @Autowired
    StatisticsMapper statisticsMapper;

    @Autowired
    PositionMapper positionMapper;

    @Override
    public void deviceTodayStatisics() {
        Log.info("统计当日设备信息");
        statisticsMapper.deviceTodayStatisics();
    }

    @Override
    public void resetTodayTrip() {
       // statisticsMapper.resetTodayTrip();
        Log.info("重置设备当日里程并更新里程到报表中");
        for(Map.Entry<Long, Position> entry:Context.positions.entrySet()){
            Position position = entry.getValue();
            Map lastAtt = (Map) JSONObject.parse(position.getAttributes());
            if(lastAtt.containsKey("todaytrip")){
                statisticsMapper.updateTodayStatisicsTrip(entry.getKey(),Double.parseDouble(lastAtt.get("todaytrip").toString()));

            }
            lastAtt.put("todaytrip",0);
            position.setAttributes(JSON.toJSONString(lastAtt));
            positionMapper.resetTodayTrip(entry.getKey(),JSON.toJSONString(lastAtt));
            Context.positions.put(entry.getKey(),position);
        }
    }

    @Override
    public List<Map> getDeviceStatisicsByUserId(long userId,String deviceIds,String column,String start,String end) {
        return statisticsMapper.getDeviceStatisicsByUserId(userId,deviceIds,column,start,end);
    }

    @Override
    public Counts getCounts() {
        return statisticsMapper.getCounts();
    }
}
